package com.dhecp.project.logstics.service.impl;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.MessageUtils;
import com.dhecp.common.utils.RandomValueUtils;
import com.dhecp.common.utils.TypeUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.constant.ModuleTable;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.core.utils.ReflectUtils;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.business.domain.FbaLogsticsOrder;
import com.dhecp.project.business.domain.FbaLogsticsOrderSelect;
import com.dhecp.project.business.mapper.FbaLogsticsOrderMapper;
import com.dhecp.project.common.domain.ModuleNID;
import com.dhecp.project.common.domain.RecordCheck;
import com.dhecp.project.common.domain.UpdateFlow;
import com.dhecp.project.common.service.impl.BaseModuleServiceImpl;
import com.dhecp.project.finance.domain.AccountCountForCompany;
import com.dhecp.project.finance.domain.AccountDetailedForCompany;
import com.dhecp.project.finance.mapper.AccountCountForCompanyMapper;
import com.dhecp.project.finance.mapper.AccountDetailedForCompanyMapper;
import com.dhecp.project.flowable.mapper.SysProcessInstanceMapper;
import com.dhecp.project.flowable.service.ActProcessService;
import com.dhecp.project.logstics.domain.*;
import com.dhecp.project.logstics.mapper.*;
import com.dhecp.project.logstics.service.FbaLogisticsReconcileEcService;
import com.dhecp.project.logstics.service.FbaLogisticsReconcileService;
import com.dhecp.project.storage.enums.DeliveryLogisticsEnum;
import com.dhecp.project.storage.enums.TailprogramEnum;
import com.dhecp.project.storage.mapper.DeliveryLogisticsMapper;
import com.dhecp.project.system.domain.SysAccountBandDept;
import com.dhecp.project.system.mapper.SysAccountBandDeptMapper;
import com.dhecp.project.system.service.SysDictionaryForFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.*;

/**
 * 物流对账单(FbaLogisticsReconcile表)服务实现类
 *
 * @author fxz
 * @since 2022-10-29 23:27:29
 */
@Service
public class FbaLgisticsReconcileEcServiceImpl extends BaseModuleServiceImpl implements FbaLogisticsReconcileEcService {
    @Autowired
    private FbaLogisticsReconcileEcMapper fbaLogisticsReconcileEcMapper;
    
    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;

    @Autowired
    private FbaLogisticsReconcileEcSonMapper fbaLogisticsReconcileEcSonMapper;

    @Autowired
    private FbaLogisticsReconcileSonMapper fbaLogisticsReconcileSonMapper;

    @Autowired
    private FbaLogisticsPaymentEcSonMapper fbaLogisticsPaymentEcSonMapper;

    @Autowired
    private AccountDetailedForCompanyMapper businessMapper;

    @Autowired
    private SysAccountBandDeptMapper sysAccountBandDeptMapper;

    @Autowired
    private AccountCountForCompanyMapper accountCountForCompanyMapper;



    /**
    * 查询列表数据
    */
    @Override
    public List<FbaLogisticsReconcileEc> queryList(Map<String, Object> params) throws Exception{
        //特殊字段fba修改查询详情有关联的数据
        if (params.get("ShipmentId") != null && !params.get("ShipmentId").equals("")){
            params.put("ShipmentId1",params.get("ShipmentId"));
            params.put("ShipmentId","");
        }
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));	//常规查询条件SQL语句
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));	//数据权限查询条件SQL语句

        if (params.get("field") == null || params.get("field") == ""){
            String orderBy = "t1.code DESC";
            PageUtils.startPageOrder(orderBy);
        }else{
            PageUtils.startPage();
        }
        return fbaLogisticsReconcileEcMapper.queryList(params);
    }


    /**
     * 通过guid查询单条数据
     *
     * @param params 对象
     * @return 实例对象
     */
    @Override
    public FbaLogisticsReconcileEc queryFirstRecord(Map<String, Object> params) throws Exception{
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));	//数据权限查询条件SQL语句
        return fbaLogisticsReconcileEcMapper.selectFirstRecordByNid(params);
    }

    @Override
    @Transactional
    public AjaxResult saveData(Map<String, Object> params) {
        params.put("businessEntity", new FbaLogisticsReconcileEc());
//        params.put("isAuto", false);
        AjaxResult ajaxResult = this.saveDataBase(params);

        Map<String, Object> infoMap = (Map<String, Object>) ajaxResult.get("infoMap");
        String resNid = infoMap.get("nid").toString();

        FbaLogisticsReconcileEc fbaLogisticsReconcileEc = JSON.parseObject(params.get("infoMap").toString(), FbaLogisticsReconcileEc.class);
        List<FbaLogisticsReconcileEcSon> onSonData = new ArrayList<>();
        if (params.containsKey("sonData")){
            onSonData = JSON.parseArray(params.get("infoItems").toString(), FbaLogisticsReconcileEcSon.class);
        }

        List<RecordCheck> guidMapList = fbaLogisticsReconcileEcMapper.findRecords(resNid);

        Map<String, Object> guidMap = new HashMap<String, Object>();
        if (!guidMapList.isEmpty()){
            if (infoMap.get("newNid") != null && "1".equals(infoMap.get("newNid").toString())){ //单号重复验证
                throw new RuntimeException(MessageUtils.message("module.error.retry"));
            }
            for (RecordCheck recordCheck : guidMapList){
                if (recordCheck != null && recordCheck.getGuid() != null){
                    guidMap.put(recordCheck.getGuid().toString(), "1");
                }
            }
        }
        FbaLogsticsOrder fbaLogsticsOrder;
        if (onSonData == null || onSonData.size() == 0){
            resNid = "0";
        }else{
            resNid = infoMap.get("nid").toString();
            String guid = "";
            if (fbaLogisticsReconcileEc.getGuid() == null){
                guid = RandomValueUtils.getGUID();
                fbaLogisticsReconcileEc.setGuid(guid);
            }else{
                guid = fbaLogisticsReconcileEc.getGuid().toString();
            }

            for (String field : infoMap.keySet()) { //遍历非网格字段，并赋值到实体中
                Object fieldValue = infoMap.get(field);
                ReflectUtils.setValue(fbaLogisticsReconcileEc, field, fieldValue);
            }

            if (guidMap.get(guid) == null){
                fbaLogisticsReconcileEc.setCode(fbaLogisticsReconcileEc.getBid());
                fbaLogisticsReconcileEc.setDeleted(false);
                fbaLogisticsReconcileEc.setActiveFlowName("制单环节");
                fbaLogisticsReconcileEcMapper.insert(fbaLogisticsReconcileEc);
            }else{
                fbaLogisticsReconcileEcMapper.update(fbaLogisticsReconcileEc);
                guidMap.remove(guid);
            }
            List<FbaLogisticsReconcileEcSon> lodData = fbaLogisticsReconcileEcSonMapper.queryListCode(fbaLogisticsReconcileEc.getCode());
            if (lodData.size()>0){
                for (FbaLogisticsReconcileEcSon item : lodData
                ) {
                    fbaLogisticsReconcileEcSonMapper.delete(item);
                    fbaLogisticsReconcileSonMapper.updateByStateEcName(item.getLogisticsOrderCode(), null);
                }
            }


            for (FbaLogisticsReconcileEcSon item : onSonData
            ) {
                item.setGuid(RandomValueUtils.getGUID());
                item.setCreatetime(new Date());
                item.setCodereconcileEc(fbaLogisticsReconcileEc.getCode());
                item.setCreateuser(ShiroUtils.getMandatorCid());
                item.setStateName(TailprogramEnum.StateName.CHECKINGACCOUNTS.value);
                fbaLogisticsReconcileEcSonMapper.insert(item);

                //已二次对账
                fbaLogisticsReconcileSonMapper.updateByStateEcName(item.getLogisticsOrderCode(), TailprogramEnum.StateName.HAVERECONCILEDEC.value);
            }

        }


        if (!guidMap.isEmpty()){
            for (String guid : guidMap.keySet()){
                List<FbaLogisticsReconcileEcSon> lodData = fbaLogisticsReconcileEcSonMapper.queryListCode(fbaLogisticsReconcileEc.getCode());
                fbaLogisticsReconcileEcSonMapper.deleteCodeReconcileEc(fbaLogisticsReconcileEc.getCode());
                fbaLogisticsReconcileEcMapper.delete(guid);

                for (FbaLogisticsReconcileEcSon fbaLogisticsReconcileEcSon : lodData){
                    fbaLogisticsReconcileSonMapper.updateByStateEcName(fbaLogisticsReconcileEcSon.getLogisticsOrderCode(), null);
                }
            }
        }
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"),resNid);
    }


    /**
     *@Description: 查询前一单NID、BID
     *@Param: [params] nid 必要参数：当前 单据编码
     *@return: com.dhecp.common.core.domain.AjaxResult  (data.nid 前一单NID， data.bid 前一单BID)
     *@Author: hlf
     *@date: 2022-05-09 10:08
     */
    @Override
    public AjaxResult queryLastNid(Map<String, Object> params) throws Exception {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        if (params.get("nid") == null || params.get("nid") == ""){
            params.put("nid", "0");
        }
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));	//数据权限查询条件SQL语句
        ModuleNID moduleNID = fbaLogisticsReconcileEcMapper.selectLastNid(params);
        if (moduleNID == null){
            dataMap.put("nid", "0");
            dataMap.put("bid", "0");
            return AjaxResult.warn(MessageUtils.message("module.warn.isfirstnid"), TypeUtils.mapToJson(dataMap).toString());
        }
        dataMap.put("nid", moduleNID.getNid().toString());
        dataMap.put("bid", moduleNID.getBid().toString());
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"), TypeUtils.mapToJson(dataMap).toString());
    }


    /**
     *@Description: 查询后一单NID、BID
     *@Param: [params] nid 必要参数：当前 单据编码
     *@return: com.dhecp.common.core.domain.AjaxResult  (data.nid 后一单NID， data.bid 后一单BID)
     *@Author: hlf
     *@date: 2022-05-09 10:09
     */
    @Override
    public AjaxResult queryNextNid(Map<String, Object> params) throws Exception {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        if (params.get("nid") == null || params.get("nid") == "" || "0".equals(params.get("nid").toString())){
            dataMap.put("nid", "0");
            dataMap.put("bid", "0");
            return AjaxResult.warn(MessageUtils.message("module.warn.isfinallynid"), TypeUtils.mapToJson(dataMap).toString());
        }
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));	//数据权限查询条件SQL语句
        ModuleNID moduleNID = fbaLogisticsReconcileEcMapper.selectNextNid(params);
        if (moduleNID == null){
            dataMap.put("nid", "0");
            dataMap.put("bid", "0");
            return AjaxResult.warn(MessageUtils.message("module.warn.isfinallynid"), TypeUtils.mapToJson(dataMap).toString());
        }
        dataMap.put("nid", moduleNID.getNid().toString());
        dataMap.put("bid", moduleNID.getBid().toString());
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"), TypeUtils.mapToJson(dataMap).toString());
    }

    /**
     *@Description: 流转  —— 添加流程任务、修改流程状态及生效标志
     *@Param: [params] tableDataId 必要参数：单据编码（nid）
     *@return: com.dhecp.common.core.domain.AjaxResult
     *@Author: hlf
     *@date: 2022-05-09 10:11
     */
    @Override
    @Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
    public AjaxResult updateFlowGo(Map<String, Object> params) {
        Map<String, Object> infoMap = new HashMap<String, Object>();
        String nid = "0";
        if (params.get("tableDataId") == null || params.get("tableDataId") ==""){
            return AjaxResult.error(MessageUtils.message("module.error.lack","单据编码【tableDataId】"));
        }else{
            nid = params.get("tableDataId").toString();
        }
        params.put("nid", nid);
        List<FbaLogisticsReconcileEc> businessDataList = fbaLogisticsReconcileEcMapper.selectByNid(params);
        if (businessDataList == null || businessDataList.size() == 0){
            return AjaxResult.error(MessageUtils.message("module.error.nodata"));
        }else{
            infoMap.put("cid", businessDataList.get(0).getCid());
            infoMap.put("bUser", businessDataList.get(0).getbUser());
            infoMap.put("bDept", businessDataList.get(0).getbDept());
            infoMap.put("procInstId", businessDataList.get(0).getProcInstId());
            infoMap.put("activeFlow", businessDataList.get(0).getActiveFlow());
            infoMap.put("createTime", businessDataList.get(0).getCreateTime());
            params.put("infoMap", TypeUtils.mapToJson(infoMap).toString());
            params.put("activeFlowOld", businessDataList.get(0).getActiveFlow());
        }

        //流程处理公共方法，返回单据（流转、回退等）后的流程状态信息
        if (params.get("optType") == null || params.get("optType") == ""){
            params.put("optType", "go");
        }
        List<AccountDetailedForCompany> accountList =new ArrayList<>();
        UpdateFlow updateFlow = new UpdateFlow();
        updateFlow = this.FlowDealBase(params);

        for (FbaLogisticsReconcileEc businessData : businessDataList) {
            updateFlow.setGuid(businessData.getGuid().toString());
            fbaLogisticsReconcileEcMapper.updateFlow(updateFlow);

            if (updateFlow.getActiveFlow().equals("end")){
                //查询详情信息
                List<FbaLogisticsReconcileEcSon> fbaLogisticsReconcileEcSonList = fbaLogisticsReconcileEcSonMapper.queryListCode(businessData.getCode());
                for (FbaLogisticsReconcileEcSon fbaLogisticsReconcileEcSon : fbaLogisticsReconcileEcSonList){
                    fbaLogisticsReconcileEcSonMapper.updateByStateName(fbaLogisticsReconcileEcSon.getLogisticsOrderCode(),TailprogramEnum.StateName.HAVERECONCILED.value);
                    // 查询物流部账号
                    SysAccountBandDept sysAccountBandDept = sysAccountBandDeptMapper.findByAccountId("WLB");
                    if (sysAccountBandDept == null){
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        return AjaxResult.error("物流部门绑定账户未启用或不存在,请核实后再操作!");
                    }
                    // 物流公司
                    AccountCountForCompany logisticsAccount = accountCountForCompanyMapper.queryFirstByVGsBm(sysAccountBandDept.getvGsBm());
                    if (logisticsAccount == null){
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        return AjaxResult.error("物流公司实时资金不存在,请核实后再操作!");
                    }
//                     物流公司渠道
                    AccountCountForCompany logisticsQdAccount = accountCountForCompanyMapper.queryFirstByVGsBm(fbaLogisticsReconcileEcSon.getCodeCompany());
                    if (logisticsQdAccount == null){
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        return AjaxResult.error("物流渠道实时资金不存在,请核实后再操作!");
                    }
                    //资金流水(物流部 -运营公司头程物流费)
                    AccountDetailedForCompany account = new AccountDetailedForCompany();
                    account.setGuid(RandomValueUtils.getGUID());
                    account.setvGsBm(logisticsAccount.getvGsBm());
                    account.setvGsMc(logisticsAccount.getvGsMc());
                    account.setvGsJc(logisticsAccount.getvGsJc());
                    account.setvGsBmP(logisticsQdAccount.getvGsBm());
                    account.setvGsMcP(logisticsQdAccount.getvGsMc());
                    account.setvGsJcP(logisticsQdAccount.getvGsJc());
                    account.setvJyLx("物流服务→头程运费支出");
                    account.setfJe(-Double.valueOf(fbaLogisticsReconcileEcSon.getLogisticsFeesDz().toString()));
                    account.setdJySj(fbaLogisticsReconcileEcSon.getCreatetime());
                    account.setdShSj(new Date());
                    account.setvBz("物流二次对账单号:"+ businessData.getCode() + "，运单号:"+ fbaLogisticsReconcileEcSon.getWaybillNumber() + "，物流订单号:" + fbaLogisticsReconcileEcSon.getLogisticsOrderCode());
                    account.setCreateTime(new Date());
                    accountList.add(account);

                    //资金流水(物流部 -运营公司头程物流费)
                    AccountDetailedForCompany account1 = new AccountDetailedForCompany();
                    account1.setGuid(RandomValueUtils.getGUID());
                    account1.setvGsBm(logisticsQdAccount.getvGsBm());
                    account1.setvGsMc(logisticsQdAccount.getvGsMc());
                    account1.setvGsJc(logisticsQdAccount.getvGsJc());
                    account1.setvGsBmP(logisticsAccount.getvGsBm());
                    account1.setvGsMcP(logisticsAccount.getvGsMc());
                    account1.setvGsJcP(logisticsAccount.getvGsJc());
                    account1.setvJyLx("物流服务→头程运费支出");
                    account1.setfJe(Double.valueOf(fbaLogisticsReconcileEcSon.getLogisticsFeesDz().toString()));
                    account1.setdJySj(fbaLogisticsReconcileEcSon.getCreatetime());
                    account1.setdShSj(new Date());
                    account1.setvBz("物流二次对账单号:"+ businessData.getCode() + "，运单号:"+ fbaLogisticsReconcileEcSon.getWaybillNumber() + "，物流订单号:" + fbaLogisticsReconcileEcSon.getLogisticsOrderCode());
                    account1.setCreateTime(new Date());
                    accountList.add(account1);
                }
            }
        }
        int insertLength = accountList.size();
        int i = 0;
        while (insertLength > 50) {
            businessMapper.insertBatchNew(accountList.subList(i, i + 50));

            i = i + 50;
            insertLength = insertLength - 50;
        }
        if (insertLength > 0) {
            businessMapper.insertBatchNew(accountList.subList(i, i + insertLength));
        }


        return AjaxResult.success(MessageUtils.message("module.success.issuccess"), nid);
    }

    /**
     *@Description: 退回  —— 添加流程任务、修改流程状态及生效标志
     *@Param: [params] tableDataId 必要参数：单据编码（nid）
     *@return: com.dhecp.common.core.domain.AjaxResult
     *@Author: hlf
     *@date: 2022-05-09 10:11
     */
    @Override
    @Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
    public AjaxResult updateFlowBack(Map<String, Object> params) {
        Map<String, Object> infoMap = new HashMap<String, Object>();
        String nid = "0";
        if (params.get("tableDataId") == null || params.get("tableDataId") ==""){
            return AjaxResult.error(MessageUtils.message("module.error.lack","单据编码【tableDataId】"));
        }else{
            nid = params.get("tableDataId").toString();
        }
        params.put("nid", nid);
        List<FbaLogisticsReconcileEc> businessDataList = fbaLogisticsReconcileEcMapper.selectByNid(params);
        if (businessDataList == null || businessDataList.size() == 0){
            return AjaxResult.error(MessageUtils.message("module.error.nodata"));
        }else{
            //判断是否被付款单使用 不允许回退
            int count = fbaLogisticsPaymentEcSonMapper.countCodereconcileEc(businessDataList.get(0).getCode());
            if (count != 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("该二次对账单已被二次付款,不允许退回!");
            }
            infoMap.put("cid", businessDataList.get(0).getCid());
            infoMap.put("bUser", businessDataList.get(0).getbUser());
            infoMap.put("bDept", businessDataList.get(0).getbDept());
            infoMap.put("procInstId", businessDataList.get(0).getProcInstId());
            infoMap.put("activeFlow", businessDataList.get(0).getActiveFlow());
            infoMap.put("createTime", businessDataList.get(0).getCreateTime());
            params.put("infoMap", TypeUtils.mapToJson(infoMap).toString());
            params.put("activeFlowOld", businessDataList.get(0).getActiveFlow());
        }

        //流程处理公共方法，返回单据（流转、回退等）后的流程状态信息
        if (params.get("optType") == null || params.get("optType") == ""){
            params.put("optType", "back");
        }
        UpdateFlow updateFlow = new UpdateFlow();
        updateFlow = this.FlowDealBase(params);
        List<AccountDetailedForCompany> accountList =new ArrayList<>();
        for (FbaLogisticsReconcileEc businessData : businessDataList) {
            updateFlow.setGuid(businessData.getGuid().toString());
            fbaLogisticsReconcileEcMapper.updateFlow(updateFlow);

            if (infoMap.get("activeFlow").equals("end")){
                //查询详情信息
                List<FbaLogisticsReconcileEcSon> fbaLogisticsReconcileEcSonList = fbaLogisticsReconcileEcSonMapper.queryListCode(businessData.getCode());
                for (FbaLogisticsReconcileEcSon fbaLogisticsReconcileEcSon : fbaLogisticsReconcileEcSonList){
                    fbaLogisticsReconcileEcSonMapper.updateByStateName(fbaLogisticsReconcileEcSon.getLogisticsOrderCode(),TailprogramEnum.StateName.CHECKINGACCOUNTS.value);
                    // 查询物流部账号
                    SysAccountBandDept sysAccountBandDept = sysAccountBandDeptMapper.findByAccountId("WLB");
                    if (sysAccountBandDept == null){
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        return AjaxResult.error("物流部门绑定账户未启用或不存在,请核实后再操作!");
                    }
                    // 物流公司
                    AccountCountForCompany logisticsAccount = accountCountForCompanyMapper.queryFirstByVGsBm(sysAccountBandDept.getvGsBm());
                    if (logisticsAccount == null){
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        return AjaxResult.error("物流公司实时资金不存在,请核实后再操作!");
                    }
//                     物流公司渠道
                    AccountCountForCompany logisticsQdAccount = accountCountForCompanyMapper.queryFirstByVGsBm(fbaLogisticsReconcileEcSon.getCodeCompany());
                    if (logisticsQdAccount == null){
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        return AjaxResult.error("物流渠道实时资金不存在,请核实后再操作!");
                    }
                    //资金流水(物流部 -运营公司头程物流费)
                    AccountDetailedForCompany account = new AccountDetailedForCompany();
                    account.setGuid(RandomValueUtils.getGUID());
                    account.setvGsBm(logisticsAccount.getvGsBm());
                    account.setvGsMc(logisticsAccount.getvGsMc());
                    account.setvGsJc(logisticsAccount.getvGsJc());
                    account.setvGsBmP(logisticsQdAccount.getvGsBm());
                    account.setvGsMcP(logisticsQdAccount.getvGsMc());
                    account.setvGsJcP(logisticsQdAccount.getvGsJc());
                    account.setvJyLx("物流服务→头程运费支出");
                    account.setfJe(Double.valueOf(fbaLogisticsReconcileEcSon.getLogisticsFeesDz().toString()));
                    account.setdJySj(fbaLogisticsReconcileEcSon.getCreatetime());
                    account.setdShSj(new Date());
                    account.setvBz("(退回)物流二次对账单号:"+ businessData.getCode() + "，运单号:"+ fbaLogisticsReconcileEcSon.getWaybillNumber() + "，物流订单号:" + fbaLogisticsReconcileEcSon.getLogisticsOrderCode());
                    account.setCreateTime(new Date());
                    accountList.add(account);

                    //资金流水(物流部 -运营公司头程物流费)
                    AccountDetailedForCompany account1 = new AccountDetailedForCompany();
                    account1.setGuid(RandomValueUtils.getGUID());
                    account1.setvGsBm(logisticsQdAccount.getvGsBm());
                    account1.setvGsMc(logisticsQdAccount.getvGsMc());
                    account1.setvGsJc(logisticsQdAccount.getvGsJc());
                    account1.setvGsBmP(logisticsAccount.getvGsBm());
                    account1.setvGsMcP(logisticsAccount.getvGsMc());
                    account1.setvGsJcP(logisticsAccount.getvGsJc());
                    account1.setvJyLx("物流服务→头程运费支出");
                    account1.setfJe(-Double.valueOf(fbaLogisticsReconcileEcSon.getLogisticsFeesDz().toString()));
                    account1.setdJySj(fbaLogisticsReconcileEcSon.getCreatetime());
                    account1.setdShSj(new Date());
                    account1.setvBz("(退回)物流二次对账单号:"+ businessData.getCode() + "，运单号:"+ fbaLogisticsReconcileEcSon.getWaybillNumber() + "，物流订单号:" + fbaLogisticsReconcileEcSon.getLogisticsOrderCode());
                    account1.setCreateTime(new Date());
                    accountList.add(account1);
                }
            }
        }
        int insertLength = accountList.size();
        int i = 0;
        while (insertLength > 50) {
            businessMapper.insertBatchNew(accountList.subList(i, i + 50));

            i = i + 50;
            insertLength = insertLength - 50;
        }
        if (insertLength > 0) {
            businessMapper.insertBatchNew(accountList.subList(i, i + insertLength));
        }
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"), nid);
    }


    /**
     * 物流对账调用数据
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public List<FbaLogsticsOrderSelect> queryListSelect(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));	//常规查询条件SQL语句
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));	//数据权限查询条件SQL语句

        if (params.get("field") == null || params.get("field") == ""){
            String orderBy = "t1.createTime asc";
            PageUtils.startPageOrder(orderBy);
        }else{
            PageUtils.startPage();
        }

        return fbaLogisticsReconcileEcMapper.findFbaLogsticsOrderSelect(params);
    }
}

